package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.BuyCarBean;
import com.project.mapper.IBuyCarMapper;
import com.project.mapper.IProductMapper;
import com.project.mapper.IRoleMapper;
import com.project.service.IBuyCarService;
import com.project.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class BuyCarServiceImpl implements IBuyCarService {
    @Autowired
    private IBuyCarMapper buyCarMapper;
    @Autowired
    private IProductMapper productMapper;

    @Override
    public List<BuyCarBean> findByUserId(Integer userId) {
        QueryWrapper<BuyCarBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("fk_userId",userId);
        List<BuyCarBean> buyCarList=buyCarMapper.selectList(queryWrapper);
        for (BuyCarBean buyCar:buyCarList){
            buyCar.setProduct(productMapper.selectById(buyCar.getProductId()));
        }
        return buyCarList;
    }

    @Override
    public void delById(Integer id) {
        buyCarMapper.deleteById(id);
    }

    @Override
    public void update(Integer id, Integer price, Integer num) {
        BuyCarBean buyCar=buyCarMapper.selectById(id);
        buyCar.setPrice(price);
        buyCar.setNum(num);
        buyCarMapper.updateById(buyCar);
    }

    @Override
    public void add(BuyCarBean buyCar) {
        buyCarMapper.insert(buyCar);
    }

    @Override
    public BuyCarBean findByProductId(Integer productId,Integer userId) {
        QueryWrapper<BuyCarBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("fk_productId", productId);
        queryWrapper.eq("fk_userId",userId);
        return buyCarMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateAdd(BuyCarBean inputBuyCar) {
        QueryWrapper<BuyCarBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("fk_productId", inputBuyCar.getProductId());
        BuyCarBean buyCar=buyCarMapper.selectOne(queryWrapper);
        buyCar.setNum(buyCar.getNum()+inputBuyCar.getNum());
        buyCarMapper.updateById(buyCar);
    }

    @Override
    public void delByUserId(Integer userId) {
        QueryWrapper<BuyCarBean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("fk_userId", userId);
        buyCarMapper.delete(queryWrapper);
    }
}
